<?php
namespace App\Controller;

class LinksController extends AppController{

    public $_page_num = 30;
    public function initialize()
    {
        parent::initialize();
    }

    /*
   * create by phpstorm
   * author：Ming
   * date：2020.04.30
   * ps：新增推荐
   * */
    public function add()
    {
        $data = $this->request->getData();
        if(empty($data)){
            $this->G->error("data_error");
            return null;
        }
        foreach ($data as $k=>$v){
            if($k == 'poster'){
                continue;
            }elseif ($k == 'enabled' || $k == 'order_num'){
                continue;
            }elseif(empty($v)){
                $this->G->error($k."_error");
                return null;
            }
        }
        $link = $this->Links->newEntity();
        $link->alias = $data['alias'];
        $link->url = $data['url'];
        $link->poster = $this->G->setImgUrl($data['poster']);
        $link->order_num = $data['order_num'];
        $link->enabled = $data['enabled'];
        $link_save = $this->Links->save($link);
        if($link_save){
            $this->loadModel('CategoryContents');
            $category_contents = $this->CategoryContents->newEntity();
            $category_contents->category_id = $data['category_id'];
            $category_contents->content_type = 'links';
            $category_contents->content_id = $link_save->id;
            $this->CategoryContents->save($category_contents);
        }
        $this->loadComponent('LinkCache');
        $this->LinkCache->deleteList();
        $this->G->success('Success');
    }

    /*
   * create by phpstorm
   * author：Ming
   * date：2020.04.30
   * ps：修改推荐
   * */
    public function update()
    {
        $data = $this->request->getData();
        if(empty($data)){
            $this->G->error("data_error");
            return null;
        }
        foreach ($data as $k=>$v){
            if($k == 'poster'){
                continue;
            }elseif ($k == 'enabled' || $k == 'order_num'){
                continue;
            }elseif(empty($v)){
                $this->G->error($k."_error");
                return null;
            }
        }
        $links = $this->Links->findById($data['id'])->select(['id'])->first();
        if(empty($links)){
            $this->G->error("link_error");
            return null;
        }
        $link = $this->Links->newEntity();
        $link->id = $data['id'];
        $link->alias = $data['alias'];
        $link->url = $data['url'];
        $link->poster = $this->G->setImgUrl($data['poster']);
        $link->order_num = $data['order_num'];
        $link->enabled = $data['enabled'];
        $this->Links->save($link);
        $this->loadModel('CategoryContents');
        $category_contents = $this->CategoryContents->find()
            ->where(['content_id'=>$data['id'],'content_type'=>'links'])
            ->select(['id'])
            ->first();
        if(!empty($category_contents)){
            $this->CategoryContents->query()
                ->update()
                ->set(['category_id'=>$data['category_id']])
                ->where(['id' => $category_contents['id']])
                ->execute();
        }
        $this->loadComponent('CategoryContentCache');
        $this->CategoryContentCache->deleteCategories('links', $data['id']);
        $this->loadComponent('LinkCache');
        $this->LinkCache->deleteCache($data['id']);
        $this->G->success('Success');
    }

    /*
   * create by phpstorm
   * author：Ming
   * date：2020.04.30
   * ps：获取推荐信息
   * */
    public function getInfo()
    {
        $id = $this->request->getData('id');
        if(!is_numeric($id)){
            $this->G->error("id_error");
            return null;
        }
        $links = $this->Links->findById($id)->first();
        if(empty($links)){
            $this->G->error("link_error");
            return null;
        }
        $this->loadComponent('CategoryContentCache');
        $cate_id = $this->CategoryContentCache->getCategories('links', $id);
        $links['category_id'] = empty($cate_id) ? 0 : $cate_id[0];
        $links['poster'] = empty($links['poster']) ? '' : $this->G->getImgUrl($links['poster']);
        $this->G->success("Success", $links);
    }

    /*
   * create by phpstorm
   * author：Ming
   * date：2020.04.30
   * ps：删除推荐
   * */
    public function delete()
    {
        $id = $this->request->getData('id');
        if(!is_numeric($id)){
            $this->G->error("id_error");
            return null;
        }
        $this->loadComponent('LinkCache');
        $this->loadComponent('CategoryContentCache');
        $this->loadModel('CategoryContents');
        $this->CategoryContentCache->deleteCategories('links', $id);
        $this->LinkCache->deleteCache($id);
        $this->LinkCache->deleteList();
        $this->Links->deleteAll(['id'=>$id]);
        $this->CategoryContents->deleteAll(['content_id'=>$id,'content_type'=>'links']);
        $this->G->success("Success");
    }

    /*
   * create by phpstorm
   * author：Ming
   * date：2020.04.30
   * ps：获取推荐列表
   * */
    public function getList()
    {
        $data = $this->request->getData();
        $this->loadComponent('LinkCache');
        if(!empty($data)){
            ksort($data, SORT_NATURAL);
        }
        $key = md5(json_encode($data));
        $data = $this->LinkCache->getList($data);
        $list = ['pageTotal'=>0,'list'=>[]];
        if(empty($data[$key]['list'])){
            $this->G->success('Success', $list);
            return null;
        }
        $list['pageTotal'] = floor($data[$key]['total']/$this->_page_num);
        foreach ($data[$key]['list'] as $k=>$v){
            $link = $this->LinkCache->getVal($v);
            if(empty($link)){
                continue;
            }
            $link['poster'] = empty($link['poster']) ? '' : $this->G->getImgUrl($link['poster']);
            $list['list'][] = $link;
        }
        $this->G->success('Success', $list);
    }

    /*
   * create by phpstorm
   * author：Ming
   * date：2020.04.30
   * ps：获取推荐的所有分类
   * */
    public function getCategories()
    {
        $this->loadComponent('SysconfigCache');
        $category_id = $this->SysconfigCache->getVal('links_category');
        $this->loadComponent('CategoryCache');
        $cache = $this->CategoryCache->getVal($category_id);
        $data = [];
        if(!empty($cache)){
            foreach ($cache as $k=>$v){
                if($v['id'] == $category_id){
                    $data = $v;
                }
            }
        }
        $this->G->success("Success", $data);
    }

}
?>